package com.zxx.coolweather._wuguan3_ok;

import android.app.Activity;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;
import okio.BufferedSink;
import okio.Okio;
import okio.Source;

/**
 * 上传进度显示
 *
 * 使用
 * new ProgressUploadFile().run();
 *
 * @author zxx on 2022/1/13
 */
public class ProgressUploadFile {

    private static final OkHttpClient okHttpClient = new OkHttpClient();

    public void run(Activity activity, final ProgressListener listener) {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);

        //caseNo: 21120255
        //phaseNo: P1
        //casePhaseId: 396243275142942720
        //filePosition: p9
        builder.addFormDataPart("caseNo", "21120255");
        builder.addFormDataPart("phaseNo", "P1");
        builder.addFormDataPart("casePhaseId", "396243275142942720");
        builder.addFormDataPart("filePosition", "p9");

        File file = new File("C:\\Users\\zhangqiangqiang\\Pictures\\TC\\图片\\1111.jpg");
        try {
            file = FileUtils.writeBytesToFile(activity.getAssets().open("1111.jpg"), "file.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
        builder.addFormDataPart("multipartFile", file.getName(), createCustomRequestBody(MultipartBody.FORM, file, new ProgressListener() {
            @Override
            public void onProgress(long totalBytes, long remainBytes, boolean done) {
                String str = (totalBytes - remainBytes) * 100 / totalBytes + "%";
                Log.e("Tag-1->", "进度：" + str);
                if (listener != null) {
                    listener.onProgress(totalBytes, remainBytes, done);
                }
            }
        }));
        builder.addFormDataPart("multipartThumbnailFile", file.getName(), createCustomRequestBody(MultipartBody.FORM, file, new ProgressListener() {
            @Override
            public void onProgress(long totalBytes, long remainBytes, boolean done) {
                //Log.e("Tag-2->", "进度：" + (totalBytes - remainBytes) * 100 / totalBytes + "%");
            }
        }));

        RequestBody body = builder.build();

        Request request = new Request.Builder()
                .url("https://hibeautynewtest.eyar.com/upload/uploadImageFile")
                .post(body)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.e("Tag-->", "响应：" + response.body().toString());
            }
        });

    }

    public static RequestBody createCustomRequestBody(final MediaType mediaType, final File file, final ProgressListener listener) {
        return new RequestBody() {
            @Override
            public MediaType contentType() {
                return mediaType;
            }

            @Override
            public long contentLength() throws IOException {
                return file.length();
            }

            @Override
            public void writeTo(BufferedSink sink) throws IOException {
                Source source;

                try {
                    source = Okio.source(file);
                    //sink.writeAll(source);
                    Buffer buf = new Buffer();
                    Long remaining = contentLength();

                    for (long readCount; (readCount = source.read(buf, 2048)) != -1; ) {
                        sink.write(buf, readCount);

                        if (listener != null)
                            listener.onProgress(contentLength(), remaining -= readCount, remaining == 0);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
    }


    public interface ProgressListener {
        void onProgress(long totalBytes, long remainBytes, boolean done);
    }

}
